package com.supor.aiot.md_msg.ui;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.AndroidViewModel;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.android.baseconfig.common.utils.Util;
import com.supor.aiot.common.adapter.VP2FragmentStateAdapter;
import com.supor.aiot.common.arouter.PathUrl;
import com.supor.aiot.common.base.BaseMVVMEnhanceActivity;
import com.supor.aiot.common.base.BaseMVVMFragment;
import com.supor.aiot.common.helper.ViewPager2Helper;
import com.supor.aiot.common.ui.view.DotIndicatorTitleView;
import com.supor.aiot.md_msg.databinding.ActivityMsgCenterBinding;
import com.supor.aiot.md_msg.model.MsgCenterModel;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;

import java.util.ArrayList;


/**
 * @author zook
 */
@Route(path = PathUrl.MsgCenter.PATH_MD_MSG_CENTER)
public class MsgCenterActivity extends BaseMVVMEnhanceActivity<MsgCenterModel, ActivityMsgCenterBinding> {

    private ArrayList<String> indicatorDatas;

    @Override
    protected void initView() {
        super.initView();
        toolBar();
        initMagicIndicator();
    }

    private void toolBar() {
        bindingView.classifyToolbar.setTitle("消息中心");
        bindingView.classifyToolbar.setTitleTextSize(Util.sp2px(this, 17));
        bindingView.classifyToolbar.setTitleColor(0xFF333333);
        bindingView.classifyToolbar.setIvLeftOnClickListener(view -> finish());
    }

    private void initMagicIndicator() {
        indicatorDatas = new ArrayList<>();
        indicatorDatas.add("服务通知");
        indicatorDatas.add("互动消息");
        indicatorDatas.add("活动");
        CommonNavigator commonNavigator = new CommonNavigator(this);
//        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return indicatorDatas == null ? 0 : indicatorDatas.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                DotIndicatorTitleView dotIndicatorTitleView = new DotIndicatorTitleView(context);
                dotIndicatorTitleView.setText(indicatorDatas.get(index));
                dotIndicatorTitleView.setTextSize(17);
                dotIndicatorTitleView.setNormalColor(Color.parseColor("#FF999999"));
                dotIndicatorTitleView.setSelectedColor(Color.parseColor("#FF333333"));

                dotIndicatorTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bindingView.vp2Content.setCurrentItem(index);
                    }
                });
                return dotIndicatorTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });


        ArrayList<BaseMVVMFragment<? extends AndroidViewModel, ? extends ViewDataBinding>> fragments = new ArrayList<>();
        fragments.add(ServiceNotificFragment.newInstance());
        fragments.add(InteractiveNotificFragment.newInstance());
        fragments.add(ActivityNotificFragment.newInstance());
        bindingView.vp2Content.setAdapter(new VP2FragmentStateAdapter<>(this, fragments));
        bindingView.vp2Content.setOffscreenPageLimit(fragments.size());
        bindingView.vp2Content.setUserInputEnabled(false);

        bindingView.magicIndicator.setNavigator(commonNavigator);

        ViewPager2Helper.bind(bindingView.magicIndicator, bindingView.vp2Content);
    }

    @Override
    protected void initData() {
        super.initData();

    }
}